<?php

namespace App\Admin\Controllers;

use \App\Models\AdminArrear;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\AdminController;

class ArrearsController extends Controller
{
    use AdminController;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('欠费管理');
            $content->description('天，我又欠钱了！！！');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(AdminArrear::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->creditor('债主名称');
            $grid->money('欠费金额');
            $grid->status('还款状况')->value(function($status){
                if($status == '2'){
                    return '已还';
                }
                return '未还';
            });
            $grid->title('标题');
            $grid->explain('说明');
            $grid->date_of_repayme('还款日期');
            $grid->created_at('创建时间');
            $grid->updated_at('更新时间');

            $grid->rows(function($row){
                if($row->status == '已还'){
                    $row->style('color:green');
                }else{
                    $row->style('color:red');
                }
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(AdminArrear::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('creditor', '债主名称');
            $form->text('money', '金额');
            $form->radio('status', '还款状况')->values([1 => '未还', 2 => '已还']);
            $form->text('title', '标题');
            $form->textarea('explain', '说明信息');
            $form->datetime('date_of_repayme', '还款日期');
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
